package com.techventus.locations;

import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class PhonePreference extends Activity{
	final Map<String,Integer> prefMap = new HashMap<String,Integer>();
	final Map<String,PrefLineView> prefLineMap = new HashMap<String,PrefLineView>();
	
	String locationName ;
	
	Button cancelButton;
	Button saveButton;
	
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.deletelocation);
		
		
		Bundle receivedBundle = getIntent().getExtras();
		locationName =receivedBundle.getString("locationName");
		
		LinearLayout outerLayout = (LinearLayout)findViewById(R.id.deletelocation);
		
		
		
		outerLayout.addView(spacer());
        TextView place = new TextView(this);
        place.setText(locationName);
        place.setTextSize(30);
        place.setGravity(Gravity.CENTER);
        
        outerLayout.addView(place);
		
        
        

  
        
        TextView minispace = new TextView(this);
        minispace.setText("  ");
        minispace.setTextSize(6);
        outerLayout.addView(minispace);
        
    	LinearLayout upper = new LinearLayout(this);
    	upper.setOrientation(0);
    	TextView PlaceHeader = new TextView(this);
    	PlaceHeader.setText("Phone");
    	PlaceHeader.setTextSize(14);
    	upper.addView(PlaceHeader);
    	PlaceHeader.setWidth(90);
    	//upper.addView(minispacer());
    	
    	TextView Enabled = new TextView(this);
    	Enabled.setText("Enable");
    	Enabled.setGravity(Gravity.LEFT);
    	Enabled.setTextSize(14);
    	Enabled.setWidth(64);
    	upper.addView(Enabled);
    	upper.addView(minispacer());
    	upper.addView(minispacer());
    	upper.addView(minispacer());

    	upper.addView(minispacer());
    	TextView Disabled = new TextView(this);
    	Disabled.setGravity(Gravity.LEFT);
    	
    	Disabled.setText("Disable");
    	Disabled.setTextSize(14);
    	Disabled.setWidth(68);
    	upper.addView(Disabled);
    	//upper.addView(spacer());
    	
    	upper.addView(minispacer());
    	
    	TextView Neutral = new TextView(this);
    	Neutral.setGravity(Gravity.CENTER_HORIZONTAL);
    	Neutral.setText("No Action");
    	Neutral.setTextSize(14);
    	Neutral.setWidth(85);
    	upper.addView(Neutral);
    	
    	
    	
    	
        outerLayout.addView(upper);
        outerLayout.addView(spacer());
       
        
        if(receivedBundle.keySet().size()<2){
        	
        }else{
			for(String phoneName: receivedBundle.keySet()){
				if(phoneName.contains("locationName")){
					
				}else{
					int pref =  receivedBundle.getInt(phoneName);
					
					prefMap.put(phoneName, pref);
					
					PrefLineView prefLineView =new PrefLineView(this,phoneName,pref);
					
					prefLineMap.put(phoneName, prefLineView);
					
		            outerLayout.addView(prefLineView);
				}
			}
		
        }
         saveButton = new Button(this);
        saveButton.setText("SAVE");
        saveButton.setWidth(150);
         cancelButton = new Button(this);
        cancelButton.setText("CANCEL");
        cancelButton.setWidth(150);
        
		cancelButton.setOnClickListener(cancelClick);
		saveButton.setOnClickListener(saveClick);
        
        
    	LinearLayout lower = new LinearLayout(this);
    	lower.setOrientation(0);
    	lower.addView(spacer());
    	lower.addView(spacer());
    	lower.addView(saveButton);
    	lower.addView(spacer());
    	lower.addView(spacer());
    	lower.addView(cancelButton);
    	outerLayout.addView(spacer());
    	outerLayout.addView(lower);
		
}
	
	
    OnClickListener saveClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
			
			for(String s:prefLineMap.keySet()){
			
				PrefLineView plv = prefLineMap.get(s);
				
				try{
					
					sql.execSQL("UPDATE LOCATIONPHONEENABLE SET phoneEnable = "+plv.getPreference()+" WHERE phoneName = '"+plv.phoneName+"' AND locationName = '"+locationName+"';");
					sql.execSQL("INSERT INTO COMMAND (command) VALUES ('rectify');");
					sql.execSQL("INSERT INTO COMMAND (command) VALUES ('enforce');");
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			sql.close();
			PhonePreference.this.finish();
		}
    };
    
    OnClickListener cancelClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			
			finish();
			
		}
    	
    };
    
	
	
	private class PrefLineView extends LinearLayout{

		protected String phoneName;
		
		private RadioGroup rg;
		private  RadioButton enabledButton;
		private RadioButton disabledButton;
		private RadioButton neutralButton;
		
		public PrefLineView(Context context, String phoneName, int pref) {
			super(context);
			
			this.phoneName = phoneName;
			
        	this.setOrientation(0);
        	
        	
            TextView phone = new TextView(context);
            phone.setText(this.phoneName);
            phone.setPadding(0, 12, 0, 0);

            phone.setWidth(95);
            this.addView(phone);

            //inner.addView(spacer());
            
            
            
            rg = new RadioGroup(context);
            rg.setOrientation(0);
           enabledButton = new RadioButton(context);
           enabledButton.setWidth(55);
            rg.addView(enabledButton);
            rg.addView(spacer());
            disabledButton = new RadioButton(context);
            rg.addView(disabledButton);
            rg.addView(spacer());
            disabledButton.setWidth(55);
            neutralButton = new RadioButton(context);
            rg.addView(neutralButton);
            neutralButton.setWidth(55);
            
            
            if(pref==1){
            	enabledButton.setChecked(true);
            }else if(pref==0){
            	neutralButton.setChecked(true);
            }else if(pref==-1){
            	disabledButton.setChecked(true);
            }
            
            
            
          //  rg.setGravity(Gravity.);
            
            this.addView(rg);
		}
		
		public int getPreference(){
			if(enabledButton.isChecked()){
				return 1;
			}else if(disabledButton.isChecked()){
				return -1;
			}else if(neutralButton.isChecked()){
				return 0;
			}else{
				return -2;
			}
		}
		
		
		
	}
	
    TextView minispacer(){
    	TextView ret = new TextView(this);
    	ret.setText("");
    	ret.setWidth(5);
    	return ret;
    }
	
    TextView spacer(){
    	TextView ret = new TextView(this);
    	ret.setText("");
    	ret.setWidth(20);
    	return ret;
    }
}